<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>自定义el-table | 张芃</title><meta name="keywords" content="VUE"><meta name="author" content="张芃"><meta name="copyright" content="张芃"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="项目需求要改变表格颜色 12345678910111213141516171819202122232425262728293031323334353637383940&lt;style&gt;&#x2F;* el-table的className是lb-table *&#x2F;&#x2F;* 边框颜色*&#x2F;.lb-table,.lb-table th,.lb-table td,.lb-table th.is-leaf,.lb-t">
<meta property="og:type" content="article">
<meta property="og:title" content="自定义el-table">
<meta property="og:url" content="https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/ElementUI/%E8%87%AA%E5%AE%9A%E4%B9%89table/index.html">
<meta property="og:site_name" content="张芃">
<meta property="og:description" content="项目需求要改变表格颜色 12345678910111213141516171819202122232425262728293031323334353637383940&lt;style&gt;&#x2F;* el-table的className是lb-table *&#x2F;&#x2F;* 边框颜色*&#x2F;.lb-table,.lb-table th,.lb-table td,.lb-table th.is-leaf,.lb-t">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com/img/avatar.jpg">
<meta property="article:published_time" content="2022-02-10T16:00:00.000Z">
<meta property="article:modified_time" content="2022-02-11T06:26:27.092Z">
<meta property="article:author" content="张芃">
<meta property="article:tag" content="VUE">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com/img/avatar.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/ElementUI/%E8%87%AA%E5%AE%9A%E4%B9%89table/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '自定义el-table',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-02-11 14:26:27'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">23</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">张芃</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">自定义el-table</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-10T16:00:00.000Z" title="发表于 2022-02-11 00:00:00">2022-02-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-11T06:26:27.092Z" title="更新于 2022-02-11 14:26:27">2022-02-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/">Vue</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/ElementUI/">ElementUI</a></span></div><div class="meta-secondline"></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><img src="/../../../../img/article/%E8%87%AA%E5%AE%9A%E4%B9%89table/%E8%87%AA%E5%AE%9A%E4%B9%89table.png" alt="img"><br>项目需求要改变表格颜色</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&lt;style&gt;</span><br><span class="line"><span class="comment">/* el-table的className是lb-table */</span></span><br><span class="line"><span class="comment">/* 边框颜色*/</span></span><br><span class="line"><span class="selector-class">.lb-table</span>,</span><br><span class="line"><span class="selector-class">.lb-table</span> <span class="selector-tag">th</span>,</span><br><span class="line"><span class="selector-class">.lb-table</span> <span class="selector-tag">td</span>,</span><br><span class="line"><span class="selector-class">.lb-table</span> <span class="selector-tag">th</span><span class="selector-class">.is-leaf</span>,</span><br><span class="line"><span class="selector-class">.lb-table--border</span>,</span><br><span class="line"><span class="selector-class">.lb-table--group</span> &#123;</span><br><span class="line">  <span class="attribute">border-color</span>: <span class="number">#2cb5d0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.lb-table--border</span><span class="selector-pseudo">::before</span>,</span><br><span class="line"><span class="selector-class">.lb-table--border</span><span class="selector-pseudo">::after</span>,</span><br><span class="line"><span class="selector-class">.lb-table--group</span><span class="selector-pseudo">::after</span>,</span><br><span class="line"><span class="selector-class">.lb-table</span><span class="selector-pseudo">::before</span>,</span><br><span class="line"><span class="selector-class">.lb-table</span><span class="selector-pseudo">::after</span> &#123;</span><br><span class="line">  <span class="attribute">background-color</span>: <span class="number">#2cb5d0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.lb-table__body</span> <span class="selector-tag">tr</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">80px</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.lb-table</span> <span class="selector-class">.cell</span> &#123;</span><br><span class="line">  <span class="attribute">white-space</span>: pre-line;</span><br><span class="line">  <span class="attribute">word-wrap</span>: break-word;</span><br><span class="line">  <span class="attribute">word-break</span>: break-all;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">2px</span> <span class="number">0</span> <span class="number">2px</span>;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">13px</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.lb-table</span> <span class="selector-tag">td</span>,</span><br><span class="line"><span class="selector-class">.lb-table</span> <span class="selector-tag">th</span> &#123;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">8px</span> <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">min-width</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>随着项目迭代,表格的行需要有一对多的子项,el-table本身是有个叫”合并行”的例子基本可以满足需求,但是对数据结构有比较严格的要求。</p>
<p>后端传过来的数据明显不合规，前端如果重新组合数据结构又会给之后的迭代留下过高的耦合。</p>
<p>最后选择在el-table的基础上用插槽实现。<br><img src="/../../../../img/article/%E8%87%AA%E5%AE%9A%E4%B9%89table/%E8%87%AA%E5%AE%9A%E4%B9%89table-2.jpg" alt="img"><br>上图可以看到，一行的高度是由上架信息或者下架信息中条数多的那个决定的。</p>
<p>举个例子，截图中的下架信息比上架信息多，那么这一行的总高度是由下架信息撑开的。做法是在<code>el-table-column</code>的<code>template</code>里循环出<code>&lt;li&gt;</code>。</p>
<p>最后把插槽中的代码封装成组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;row.upperAndLowerRecordList.length==0&quot;</span>&gt;</span>-<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span></span></span><br><span class="line"><span class="tag">      <span class="attr">v-for</span>=<span class="string">&quot;(item,i) in row.upperAndLowerRecordList&quot;</span></span></span><br><span class="line"><span class="tag">      <span class="attr">:key</span>=<span class="string">&quot;i&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;height:100%;&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">v-bind:style</span>=<span class="string">&quot;comStyle(row.upperAndLowerRecordList.length,row.upperAndLowerRecordList1.length)&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">HR</span> <span class="attr">v-if</span>=<span class="string">&quot;i&gt;0&amp;&amp;i&lt;row.upperAndLowerRecordList.length&quot;</span> <span class="attr">align</span>=<span class="string">center</span> <span class="attr">width</span>=<span class="string">200%</span> <span class="attr">color</span>=<span class="string">#2cb5d0</span> <span class="attr">size</span>=<span class="string">1</span> <span class="attr">style</span>=<span class="string">&quot;margin:8px -2px;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">:style</span>=<span class="string">&quot;numberStyle(item[field])&quot;</span>&gt;</span>&#123;&#123;item[field]&#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">v-if</span>=<span class="string">&quot;showLabel!==&#x27;&#x27;&quot;</span> <span class="attr">:style</span>=<span class="string">&quot;numberStyle(item[field])&quot;</span>&gt;</span>&#123;&#123;showLabel&#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="language-javascript">  <span class="comment">// 父组件传过来的值</span></span></span><br><span class="line"><span class="language-javascript">  <span class="attr">props</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">row</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">type</span>: <span class="title class_">Object</span>,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">default</span>: <span class="function">() =&gt;</span> &#123;&#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">field</span>: &#123; <span class="comment">//upperAndLowerRecordList1 里的字段</span></span></span><br><span class="line"><span class="language-javascript">      <span class="attr">type</span>: <span class="title class_">String</span>,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">default</span>: <span class="function">() =&gt;</span> <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">showLabel</span>: &#123; <span class="comment">// 直接显示</span></span></span><br><span class="line"><span class="language-javascript">      <span class="attr">type</span>: <span class="title class_">String</span>,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">default</span>: <span class="function">() =&gt;</span> <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">color</span>:&#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">type</span>: <span class="title class_">String</span>,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">default</span>:<span class="function">() =&gt;</span> <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">    &#125; </span></span><br><span class="line"><span class="language-javascript">  &#125;,</span></span><br><span class="line"><span class="language-javascript">  <span class="title function_">data</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">return</span>&#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">colors</span>:&#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="attr">red</span>:<span class="string">&#x27;#f56c6c&#x27;</span>,</span></span><br><span class="line"><span class="language-javascript">        <span class="attr">green</span>:<span class="string">&#x27;#10cc55&#x27;</span>,</span></span><br><span class="line"><span class="language-javascript">        <span class="attr">blue</span>:<span class="string">&#x27;#4171f7&#x27;</span></span></span><br><span class="line"><span class="language-javascript">      &#125;,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">textStyle</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    </span></span><br><span class="line"><span class="language-javascript">  &#125;,</span></span><br><span class="line"><span class="language-javascript">  <span class="title function_">mounted</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">colors</span>!==<span class="string">&#x27;&#x27;</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="variable language_">this</span>.<span class="property">textStyle</span> =<span class="variable language_">this</span>.<span class="property">colors</span>[<span class="variable language_">this</span>.<span class="property">color</span>]</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">  &#125;,</span></span><br><span class="line"><span class="language-javascript">  <span class="attr">computed</span>:&#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">comStyle</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="keyword">return</span> <span class="keyword">function</span> (<span class="params">length, upLength</span>) &#123; <span class="comment">//前少,后多</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> height = <span class="number">0</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (length &gt; <span class="number">0</span> &amp;&amp; upLength &gt; length) &#123;</span></span><br><span class="line"><span class="language-javascript">          height = (<span class="number">40</span> * upLength - <span class="number">16</span>) / length - <span class="number">8</span></span></span><br><span class="line"><span class="language-javascript">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (upLength === length) &#123;</span></span><br><span class="line"><span class="language-javascript">          height = <span class="number">32</span></span></span><br><span class="line"><span class="language-javascript">        &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">          height = <span class="number">32</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="language-javascript">          <span class="attr">height</span>: height + <span class="string">&#x27;px&#x27;</span>,</span></span><br><span class="line"><span class="language-javascript">          <span class="attr">margin</span>: <span class="string">&#x27;0 auto&#x27;</span>,</span></span><br><span class="line"><span class="language-javascript">          <span class="string">&#x27;line-height&#x27;</span>: height + <span class="string">&#x27;px&#x27;</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">numberStyle</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="keyword">return</span> <span class="keyword">function</span> (<span class="params">num</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> color = <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">color</span>===<span class="string">&#x27;&#x27;</span>) &#123; <span class="comment">//没传默认不加颜色</span></span></span><br><span class="line"><span class="language-javascript">          <span class="keyword">return</span> &#123;<span class="attr">color</span>:<span class="string">&#x27;&#x27;</span>&#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (num&lt;<span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">          color = <span class="variable language_">this</span>.<span class="property">colors</span>.<span class="property">red</span></span></span><br><span class="line"><span class="language-javascript">        &#125;<span class="keyword">else</span>&#123;</span></span><br><span class="line"><span class="language-javascript">          color = <span class="variable language_">this</span>.<span class="property">textStyle</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> &#123;<span class="attr">color</span>:color&#125;</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">  &#125;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">张芃</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/ElementUI/%E8%87%AA%E5%AE%9A%E4%B9%89table/">https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/ElementUI/%E8%87%AA%E5%AE%9A%E4%B9%89table/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhangpeng-1304089575.cos-website.ap-shanghai.myqcloud.com" target="_blank">张芃</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/VUE/">VUE</a></div><div class="post_share"><div class="social-share" data-image="/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E7%89%B9%E6%80%A7%E6%80%BB%E7%BB%93/"><img class="prev-cover" src="/img/vue.svg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue特性总结</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/10/%E5%A4%A7%E5%89%8D%E7%AB%AF/CSS/%E8%A7%A3%E5%86%B3node-sass%E7%9A%84%E5%AE%89%E8%A3%85%E9%97%AE%E9%A2%98/"><img class="next-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">解决node-sass的安装问题</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E4%B8%ADkey%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/" title="Vue中key的工作原理"><img class="cover" src="/img/vue.svg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-11</div><div class="title">Vue中key的工作原理</div></div></a></div><div><a href="/2021/10/19/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E5%88%86%E7%8E%AF%E5%A2%83%E6%89%93%E5%8C%85/" title="Vue分环境打包"><img class="cover" src="/img/vue.svg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-19</div><div class="title">Vue分环境打包</div></div></a></div><div><a href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6/" title="Vue函数式组件"><img class="cover" src="/img/vue.svg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-11</div><div class="title">Vue函数式组件</div></div></a></div><div><a href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E7%89%B9%E6%80%A7%E6%80%BB%E7%BB%93/" title="Vue特性总结"><img class="cover" src="/img/vue.svg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-11</div><div class="title">Vue特性总结</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">张芃</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">23</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/zhangpeng_git"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E4%B8%ADkey%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/" title="Vue中key的工作原理"><img src="/img/vue.svg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue中key的工作原理"/></a><div class="content"><a class="title" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E4%B8%ADkey%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/" title="Vue中key的工作原理">Vue中key的工作原理</a><time datetime="2022-02-10T16:00:00.000Z" title="发表于 2022-02-11 00:00:00">2022-02-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6/" title="Vue函数式组件"><img src="/img/vue.svg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue函数式组件"/></a><div class="content"><a class="title" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6/" title="Vue函数式组件">Vue函数式组件</a><time datetime="2022-02-10T16:00:00.000Z" title="发表于 2022-02-11 00:00:00">2022-02-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E7%89%B9%E6%80%A7%E6%80%BB%E7%BB%93/" title="Vue特性总结"><img src="/img/vue.svg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue特性总结"/></a><div class="content"><a class="title" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/Vue%E7%89%B9%E6%80%A7%E6%80%BB%E7%BB%93/" title="Vue特性总结">Vue特性总结</a><time datetime="2022-02-10T16:00:00.000Z" title="发表于 2022-02-11 00:00:00">2022-02-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/02/11/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/ElementUI/%E8%87%AA%E5%AE%9A%E4%B9%89table/" title="自定义el-table">自定义el-table</a><time datetime="2022-02-10T16:00:00.000Z" title="发表于 2022-02-11 00:00:00">2022-02-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/10/%E5%A4%A7%E5%89%8D%E7%AB%AF/CSS/%E8%A7%A3%E5%86%B3node-sass%E7%9A%84%E5%AE%89%E8%A3%85%E9%97%AE%E9%A2%98/" title="解决node-sass的安装问题"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="解决node-sass的安装问题"/></a><div class="content"><a class="title" href="/2022/02/10/%E5%A4%A7%E5%89%8D%E7%AB%AF/CSS/%E8%A7%A3%E5%86%B3node-sass%E7%9A%84%E5%AE%89%E8%A3%85%E9%97%AE%E9%A2%98/" title="解决node-sass的安装问题">解决node-sass的安装问题</a><time datetime="2022-02-09T16:00:00.000Z" title="发表于 2022-02-10 00:00:00">2022-02-10</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 张芃</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script></div></body></html>